/*
 * @Filename: teamController.java
 * @CreateTime: 2015-11-13 14:16:44
 * @Version: 1.0
 * @All rights (are) reserved.
 * @author miklechun
 */
package com.wonders.qxmanager.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.wonders.qxmanager.base.Parameter;
import com.wonders.qxmanager.model.Team;
import com.wonders.qxmanager.service.TeamService;
import com.wonders.qxmanager.utils.PageArrayList;
import com.wonders.qxmanager.utils.PageUIHelper;
import com.wonders.qxmanager.utils.PageUIHelper.Page;

/**
 * team web控制器
 * 
 * @author miklechun
 * 
 */
@Controller
public class TeamController {

	@Autowired
	private TeamService teamService;

	@RequestMapping("/team/index.do")
	public ModelAndView index(HttpServletRequest req, HttpServletResponse res)
			throws IOException {
		return new ModelAndView("/team/list.jsp");
	}

	@RequestMapping("/team/edit.do")
	public void edit(Team team,HttpServletRequest req, HttpServletResponse res)
			throws Exception {
		
		String sqledit = "UPDATE Team set number=:p1,name=:p2,pid=:p3 WHERE userid=:p0";
		Parameter parameter1 = new Parameter();
		System.out.println(team.getUserid()+"=============");
		parameter1.put("p0", team.getUserid());
		parameter1.put("p1", team.getNumber());
		parameter1.put("p2", team.getName());
		parameter1.put("p3", team.getPid());
		this.teamService.updateTeam(sqledit, parameter1);
		JSONObject result=new JSONObject();
		result.put("success", true);
		PageUIHelper.writeFormResponse(res, result.toString());
		
		
		/*Team team = new Team();
		team.setUserid(Integer.parseInt(req.getParameter("userid")));
		System.out.println(req.getParameter("name")+"===================");
		team.setName(req.getParameter("name"));
		System.out.println(req.getParameter("userid")+"===================");
		team.setNumber(Integer.parseInt(req.getParameter("number")));
		System.out.println(req.getParameter("pid")+"===================");
		team.setPid(req.getParameter("pid"));
		this.teamService.updateTeam(team);
		JSONObject result=new JSONObject();
		result.put("success", true);
		res.setContentType("text/html;charset=utf-8");
		PrintWriter out=res.getWriter();
		out.println(result.toString());
		out.flush();
		out.close();*/
	}

	@RequestMapping("/team/save.do")
	public void save(HttpServletRequest req, HttpServletResponse res)
			throws Exception {
		Team team = new Team();
		team.setName(req.getParameter("name"));
		team.setNumber(Integer.parseInt(req.getParameter("number")));
		team.setPid(req.getParameter("pid"));
		this.teamService.addTeam(team);
		JSONObject result=new JSONObject();
		result.put("success", true);
		PageUIHelper.writeFormResponse(res, result.toString());
		System.out.println(result.toString()+"================");
	}

	@RequestMapping("/team/remove.do")
	public void remove(@RequestParam(value="ids")String ids,HttpServletRequest req, HttpServletResponse res) throws IOException {
		String []idsStr=ids.split(",");
		for(int i=0;i<idsStr.length;i++){
			this.teamService.deleteTeam(Integer.parseInt(idsStr[i]));
		}
		JSONObject result=new JSONObject();
		result.put("success", true);
		res.setContentType("text/html;charset=utf-8");
		PrintWriter out=res.getWriter();
		out.println(result.toString());
		out.flush();
		out.close();
		/*JSONObject result=new JSONObject();
		result.put("success", true);
		PageUIHelper.writeFormResponse(res, result.toString());*/
	}

	@RequestMapping("/team/list.do")
	public void list(HttpServletRequest req, HttpServletResponse res)
			throws IOException {
		Team team = new Team();
		team.setName(req.getParameter("name"));
		Page page = PageUIHelper.getPage(req);
		PageArrayList<Team> list = teamService.getListByPage(team, page);
		PageUIHelper.writeJsonResponse(res, PageUIHelper.toString(list));
	}
	
}
